package com.huluobo.h3kotlindemo.detail

import android.content.Context
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.widget.ImageView
import androidx.lifecycle.ViewModelProvider
import com.blankj.utilcode.util.ToastUtils
import com.bumptech.glide.Glide
import com.huluobo.h3kotlindemo.R
import com.huluobo.h3kotlindemo.base.BaseMVVMActivity
import com.huluobo.h3kotlindemo.databinding.ActivityDetailBinding
import com.huluobo.h3kotlindemo.net.Const
import com.youth.banner.BannerConfig
import com.youth.banner.loader.ImageLoader

class DetailActivity : BaseMVVMActivity<ActivityDetailBinding>() {
    private var goodsId = 0
    private lateinit var detailViewModel: DetailViewModel
    override fun initData() {
        goodsId = intent.getIntExtra(Const.PARAM_GOODS_ID, 0)
        detailViewModel = ViewModelProvider(this)[DetailViewModel::class.java]

        if (goodsId != 0) {
            detailViewModel.getGoodsDetail(goodsId)
        }

        detailViewModel.detailSuccess.observe(this) {
            if (it.code == 200) {
                dataBinding.detailBanner.setImages(it.data.bannerList)
                dataBinding.detailBanner.start()

                dataBinding.detailDesc.text = it.data.goods_desc

                dataBinding.detailPrice.text = "团购价:￥${it.data.goods_default_price}元"

                com.huluobo.h3kotlindemo.util.ImageLoader.loadImage(
                    this,
                    it.data.goods_detail_one,
                    dataBinding.detailDetailImg1
                )

                com.huluobo.h3kotlindemo.util.ImageLoader.loadImage(
                    this,
                    it.data.goods_detail_two,
                    dataBinding.detailDetailImg2
                )
            }
        }

        detailViewModel.addToCartSuccess.observe(this) {
            ToastUtils.showLong(it.message)
        }
    }

    override fun initView() {
        dataBinding.detailBanner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR)
        dataBinding.detailBanner.setImageLoader(object : ImageLoader() {
            override fun displayImage(context: Context?, path: Any?, imageView: ImageView?) {
                if (context != null && imageView != null) {
                    Glide.with(context).load(path).into(imageView)
                }
            }
        })
        dataBinding.detailBanner.setDelayTime(1500)
        dataBinding.detailBanner.setIndicatorGravity(BannerConfig.CENTER)

        dataBinding.detailAddToCart.setOnClickListener {
            detailViewModel.addToCart(goodsId, 1)
        }

    }

    override fun getLayoutId(): Int = R.layout.activity_detail
}